package mcommand;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 * Sets how the MainPanel should look like, how the highscore should be painted,
 * how to handle the states for the MainPanel and holds a method to refresh the
 * highscore from a the local highscore XML file
 *
 * @author Marcel, Christian, Klaus
 */
@SuppressWarnings("serial")
public class MainPanel extends JPanel {
    // sets a variable where to find the startscreen and the instructions for
    // the help
    private final String PATH_IMAGE_SPLASHSCREEN = "assets/startup2.jpg";
    private final String PATH_IMAGE_HELPSCREEN = "assets/instructions.png";

    // defines a MainPanelState
    private MainPanelState state;

    // defines Image variables for the instructions and the startscreen
    private Image splashScreen;
    private Image helpScreen;

    // defines a ArrayList for the highscore units
    private Highscore highscore;
    private ArrayList<HighscoreUnit> highscoreUnits;

    // sets a String variable with the text "Highscore"
    private String highscoreText = "Highscore";

    // sets where the positions should be for the highscore label
    private final int HIGHSCORE_X = 232;
    private final int HIGHSCORE_Y = 20;

    // sets variables for positions for the playername and the points
    private final int HIGHSCORE_PLAYER_X = 180;
    private final int HIGHSCORE_POINTS_X = 300;
    private final int HIGHSCORE_PLAYER_Y = 50;

    // sets the position for the statistics
    private static final int STATISTICS_X = 130;
    private static final int STATISTICS_Y = 250;

    // defines a new menuPanel
    private MenuPanel menuPanel;

    /**
     * Tries to load the instructions image for help and the start image defines
     * this has no default layout, the background is black and the
     * MainPanelState is SPLASH (first show the start image).
     */
    public MainPanel() {
        // sets that the background is black
        this.setBackground(Color.BLACK);

        // sets the MainPanelState to SPLASH
        state = MainPanelState.SPLASH;

        // sets that this has no default layout
        this.setLayout(null);

        // sets up a new highscore
        highscore = new Highscore(true);

        // tries to load the splashscreen image
        try {
            ImageIcon ii = new ImageIcon(this.getClass().getResource(
                    PATH_IMAGE_SPLASHSCREEN));
            splashScreen = ii.getImage();

            // prints a message on the console if something went wrong
        } catch (Exception e) {
            System.err.println("HelpScreen (" + PATH_IMAGE_HELPSCREEN
                    + ") konnte nicht geladen werden.");
        }

        // tries to load the instructions image for the help
        try {
            ImageIcon ii = new ImageIcon(this.getClass().getResource(
                    PATH_IMAGE_HELPSCREEN));
            helpScreen = ii.getImage();

            // prints a message on the console if something went wrong
        } catch (Exception e) {
            System.err.println("HelpScreen (" + PATH_IMAGE_HELPSCREEN
                    + ") konnte nicht geladen werden.");
        }
    }

    /**
     * Draws the instructions image if the MainPanelState is HELP or draws start
     * image if the state is SPLASH or draws the highscore if the state is
     * HIGHSCORE.
     * 
     * @param g g
     */
    public final void paint(final Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;

        if (state == MainPanelState.SPLASH) {
            g2d.drawImage(splashScreen, 0, 0, null);
        } else if (state == MainPanelState.HIGHSCORE) {
            drawHighscore(g2d);
        } else if (state == MainPanelState.HELP) {
            g2d.drawImage(helpScreen, 0, 0, null);
        }

        // repaints
        repaint();
    }

    /**
     * Sets the menupanel.
     * @param menupanel menupanel
     */
    public final void setMenuPanel(final MenuPanel menupanel) {
        this.menuPanel = menupanel;
    }

    /**
     * Refreshes the highscore from the local XML to the ArrayList
     * highscoreUnits.
     */
    private void refreshHighscoreFromXML() {
        highscore.refresh();
    }

    /**
     * Draws the highscore label on top of the MainPanel. Draws the highscore
     * with the data from the ArrayList highscoreUnits.
     * 
     * @param g2d g2d
     */
    private void drawHighscore(final Graphics2D g2d) {
        // sets the color for painting to white
        g2d.setColor(Color.WHITE);

        // draws the label with the text "Highscore"
        g2d.drawString(highscoreText, HIGHSCORE_X, HIGHSCORE_Y);

        // sets up highscoreUnits with the highscore data
        highscoreUnits = highscore.getHighscore();

        // if ArrayList is not empty
        if (highscoreUnits.size() != 0) {
            // for every entry in the ArrayList
            for (int i = 0; i < highscoreUnits.size(); i++) {
                // defines the Playername with the position
                String player = (i + 1) + ". "
                        + highscoreUnits.get(i).playername;
                // defines the points for the player of this round
                String points = String.valueOf((highscoreUnits.get(i).score));

                // draws the second variables seted abouve
                g2d.drawString(player, HIGHSCORE_PLAYER_X, HIGHSCORE_PLAYER_Y
                        + (i * 18));
                g2d.drawString(points, HIGHSCORE_POINTS_X, HIGHSCORE_PLAYER_Y
                        + (i * 18));
            }
        }

        // draws the statistic
        g2d.drawString("Statistics Rockets fired: "
                + Statistics.getRocketCounter() + "   "
                + "Rockets hit: " + Statistics.getRocketHitCounter(),
                STATISTICS_X, STATISTICS_Y);
    }

    /**
     * Sets the MainPanelState.
     * If it is HIGHSCORE it refreshes also the
     * ArrayList from the XML
     * 
     * @param state state
     */
    public final void setState(final MainPanelState state) {
        this.state = state;

        if (state == MainPanelState.HIGHSCORE) {
            if (menuPanel != null) {
                menuPanel.backFromGame();
            }

            refreshHighscoreFromXML();
            Statistics.readStatisticsFromXML();
        }
    }

    /**
     * Returns the current MainPanelState.
     * 
     * @return state
     */
    public final MainPanelState getState() {
        return state;
    }

    /**
     * Holds the possible MainPanelStates (SPLASH, HIGHSCORE, HELP).
     * 
     */
    public enum MainPanelState {
        SPLASH,
        HIGHSCORE,
        HELP
    }

}
